package gov.fema.hazus.bdc.server.servlet;

import gov.fema.hazus.bdc.server.servlet.DAO;
import gov.fema.hazus.bdc.shared.entity.AEBMResults;
import gov.fema.hazus.bdc.shared.entity.Address;
import gov.fema.hazus.bdc.shared.entity.Bracing;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.Capacity;
import gov.fema.hazus.bdc.shared.entity.Contact;
import gov.fema.hazus.bdc.shared.entity.EarthquakeBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.EarthquakeHazard;
import gov.fema.hazus.bdc.shared.entity.EarthquakeSecondaryCharacteristics;
import gov.fema.hazus.bdc.shared.entity.Economy;
import gov.fema.hazus.bdc.shared.entity.FloodBasement;
import gov.fema.hazus.bdc.shared.entity.FloodBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.FloodHazard;
import gov.fema.hazus.bdc.shared.entity.Location;
import gov.fema.hazus.bdc.shared.entity.ManufacturedHousing;
import gov.fema.hazus.bdc.shared.entity.Roof;
import gov.fema.hazus.bdc.shared.entity.WallsAndOpenings;
import gov.fema.hazus.bdc.shared.entity.WindBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.WindHazard;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions;
import com.google.appengine.api.labs.taskqueue.TaskOptions.Method;
import com.google.inject.Singleton;
import com.googlecode.objectify.OQuery;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

@Singleton
public class GetBuildingObjectify extends HttpServlet {

	public static final String TASK_PATH = "/task/objectify/building/get";

	private final Logger logger = Logger.getLogger(GetBuildingObjectify.class);
    private Cache cache;

    private DAO dao = new DAO();
    
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	 throws ServletException, IOException {
		

		doGet(req, resp);
		
		
	 }
	
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
        
    	res.setContentType("text/plain");
		
		String buildingId = req.getParameter("id");
		long id = Long.parseLong(buildingId);
		
		try {
			
			Objectify ofy = ObjectifyService.begin();
			OQuery<Building> q = ObjectifyService.createQuery(Building.class);
			q.sort("name"); // ascending sort on building name
			q.filter("id == ", id);
			
			Building fetched = (Building) ofy.prepare(q).asSingle();

			OQuery<AEBMResults> queryAEBMResults = ObjectifyService.createQuery(AEBMResults.class);
			queryAEBMResults.filter("id == ", fetched.getId());
			AEBMResults aEBMResults = (AEBMResults) ofy.prepare(queryAEBMResults).asSingle();
			fetched.setAebmResults(aEBMResults);

			OQuery<Address> queryAddress = ObjectifyService.createQuery(Address.class);
			queryAddress.filter("id == ", fetched.getId());
			Address address = (Address) ofy.prepare(queryAddress).asSingle();
			fetched.setAddress(address);

			OQuery<Location> queryLocation = ObjectifyService.createQuery(Location.class);
			queryLocation.filter("id == ", fetched.getId());
			Location location = (Location) ofy.prepare(queryLocation).asSingle();
			fetched.setLocation(location);

			OQuery<Contact> queryContact = ObjectifyService.createQuery(Contact.class);
			queryContact.filter("id == ", fetched.getId());
			Contact contact = (Contact) ofy.prepare(queryContact).asSingle();
			fetched.setContact(contact);

			OQuery<Economy> queryEconomy = ObjectifyService.createQuery(Economy.class);
			queryEconomy.filter("id == ", fetched.getId());
			Economy economy = (Economy) ofy.prepare(queryEconomy).asSingle();
			fetched.setEconomy(economy);

			OQuery<Capacity> queryCapacity = ObjectifyService.createQuery(Capacity.class);
			queryCapacity.filter("id == ", fetched.getId());
			Capacity capacity = (Capacity) ofy.prepare(queryCapacity).asSingle();
			fetched.setCapacity(capacity);

			OQuery<Bracing> queryBracing = ObjectifyService.createQuery(Bracing.class);
			queryBracing.filter("id == ", fetched.getId());
			Bracing bracing = (Bracing) ofy.prepare(queryBracing).asSingle();
			fetched.setBracing(bracing);
			
			OQuery<EarthquakeHazard> queryEarthquakeHazard = ObjectifyService.createQuery(EarthquakeHazard.class);
			queryEarthquakeHazard.filter("id == ", fetched.getId());
			EarthquakeHazard earthquakeHazard = (EarthquakeHazard) ofy.prepare(queryEarthquakeHazard).asSingle();
			fetched.setEarthquakeHazard(earthquakeHazard);

			OQuery<EarthquakeBuildingDesign> queryEarthquakeBuildingDesign = ObjectifyService.createQuery(EarthquakeBuildingDesign.class);
			queryEarthquakeBuildingDesign.filter("id == ", fetched.getId());
			EarthquakeBuildingDesign earthquakeBuildingDesign = (EarthquakeBuildingDesign) ofy.prepare(queryEarthquakeBuildingDesign).asSingle();
			fetched.setEarthquakeBuildingDesign(earthquakeBuildingDesign);

			OQuery<EarthquakeSecondaryCharacteristics> queryEarthquakeSecondaryCharacteristics = ObjectifyService.createQuery(EarthquakeSecondaryCharacteristics.class);
			queryEarthquakeSecondaryCharacteristics.filter("id == ", fetched.getId());
			EarthquakeSecondaryCharacteristics earthquakeSecondaryCharacteristics = (EarthquakeSecondaryCharacteristics) ofy.prepare(queryEarthquakeSecondaryCharacteristics).asSingle();
			fetched.setEarthquakeSecondaryCharacteristics(earthquakeSecondaryCharacteristics);

			OQuery<FloodBasement> queryFloodBasement = ObjectifyService.createQuery(FloodBasement.class);
			queryFloodBasement.filter("id == ", fetched.getId());
			FloodBasement floodBasement = (FloodBasement) ofy.prepare(queryFloodBasement).asSingle();
			fetched.setFloodBasement(floodBasement);

			OQuery<FloodBuildingDesign> queryFloodBuildingDesign = ObjectifyService.createQuery(FloodBuildingDesign.class);
			queryFloodBuildingDesign.filter("id == ", fetched.getId());
			FloodBuildingDesign floodBuildingDesign = (FloodBuildingDesign) ofy.prepare(queryFloodBuildingDesign).asSingle();
			fetched.setFloodBuildingDesign(floodBuildingDesign);

			OQuery<FloodHazard> queryFloodHazard = ObjectifyService.createQuery(FloodHazard.class);
			queryFloodHazard.filter("id == ", fetched.getId());
			FloodHazard floodHazard = (FloodHazard) ofy.prepare(queryFloodHazard).asSingle();
			fetched.setFloodHazard(floodHazard);

			OQuery<ManufacturedHousing> queryManufacturedHousing = ObjectifyService.createQuery(ManufacturedHousing.class);
			queryManufacturedHousing.filter("id == ", fetched.getId());
			ManufacturedHousing manufacturedHousing = (ManufacturedHousing) ofy.prepare(queryManufacturedHousing).asSingle();
			fetched.setManufacturedHousing(manufacturedHousing);

			OQuery<Roof> queryRoof = ObjectifyService.createQuery(Roof.class);
			queryRoof.filter("id == ", fetched.getId());
			Roof roof = (Roof) ofy.prepare(queryRoof).asSingle();
			fetched.setRoof(roof);

			OQuery<WallsAndOpenings> queryWallsAndOpenings = ObjectifyService.createQuery(WallsAndOpenings.class);
			queryWallsAndOpenings.filter("id == ", fetched.getId());
			WallsAndOpenings wallsAndOpenings = (WallsAndOpenings) ofy.prepare(queryWallsAndOpenings).asSingle();
			fetched.setWallsAndOpenings(wallsAndOpenings);

			OQuery<WindBuildingDesign> queryWindBuildingDesign = ObjectifyService.createQuery(WindBuildingDesign.class);
			queryWindBuildingDesign.filter("id == ", fetched.getId());
			WindBuildingDesign windBuildingDesign = (WindBuildingDesign) ofy.prepare(queryWindBuildingDesign).asSingle();
			fetched.setWindBuildingDesign(windBuildingDesign);

			OQuery<WindHazard> queryWindHazard = ObjectifyService.createQuery(WindHazard.class);
			queryWindHazard.filter("id == ", fetched.getId());
			WindHazard windHazard = (WindHazard) ofy.prepare(queryWindHazard).asSingle();
			fetched.setWindHazard(windHazard);
			
			res.getWriter().println("fetched=" + fetched);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	
    }
    
	
}
